﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Moonwish.Silverlight.Controls
{
    public class ContentPlaceHolder : ContentControl, INotifyPropertyChanged
    {
        private TextBlock _textBlock;
        private ContentPresenter _headerContent;
        public static readonly DependencyProperty HeaderProperty =
           DependencyProperty.Register("Header", typeof(string), typeof(ContentPlaceHolder),
                                       new PropertyMetadata(HeaderChanged));
        public static readonly DependencyProperty HeaderContentProperty =
            DependencyProperty.Register("HeaderContent", typeof(object), typeof(ContentPlaceHolder),
                                        new PropertyMetadata(HeaderContentChanged));

        public ContentPlaceHolder()
        {
            DefaultStyleKey = typeof(ContentPlaceHolder);
        }

        public string Header
        {
            get { return (string)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public object HeaderContent
        {
            get { return GetValue(HeaderContentProperty); }
            set { SetValue(HeaderContentProperty, value); }
        }

        public Visibility HeaderVisibility
        {
            get
            {
                return (Visibility)GetValue(HeaderVisibilityProperty);
            }
            set
            {
                SetValue(HeaderVisibilityProperty, value);
            }
        }
        public static readonly DependencyProperty HeaderVisibilityProperty =
           DependencyProperty.Register("HeaderVisibility", typeof(Visibility), typeof(ContentPlaceHolder),
                                       new PropertyMetadata(Visibility.Visible));

        private static void HeaderChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var ownerObj = (ContentPlaceHolder)obj;
            ownerObj.OnPropertyChanged("Header");
        }

        private static void HeaderContentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var ownerObj = (ContentPlaceHolder)obj;
            ownerObj.OnPropertyChanged("HeaderContent");
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _textBlock = (TextBlock)GetTemplateChild("textBlock");
            _headerContent = (ContentPresenter)GetTemplateChild("headerContent");
            if (string.IsNullOrEmpty(Header))
            {
                _textBlock.Visibility = Visibility.Collapsed;
                _headerContent.Visibility = Visibility.Visible;
            }
            else
            {
                _textBlock.Visibility = Visibility.Visible;
                _headerContent.Visibility = Visibility.Collapsed;
            }
            if (HeaderVisibility == Visibility.Collapsed)
            {
                var rootLayout = (Grid) GetTemplateChild("RootLayout");
                var body = (Border) GetTemplateChild("Body");

                rootLayout.RowDefinitions[0].Height = new GridLength(0);
                body.BorderThickness = new Thickness(1);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}
